/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "prefsqueries.h"
#include "newquery.h"
#include <QIcon>
#include <QMessageBox>

void PrefsQueries::on_tQueries_currentCellChanged(int row)
{
	bDown->setEnabled(row < queryList.count()-1);
	bUp->setEnabled(row > 0);
	bRemove->setEnabled(row >= 0);
	bEdit->setEnabled(row >= 0);
}

void PrefsQueries::on_bDown_clicked()
{
	int cnt = queryList.count();
	int i = tQueries->currentRow();
	if ((i < 0) || (i > cnt-2)) return;

	Query::Prefs tmp = queryList[i];
	queryList[i] = queryList[i+1];
	queryList[i+1] = tmp;
	tQueries->setCurrentCell(i+1, tQueries->currentColumn());

	refresh();
}

void PrefsQueries::on_bUp_clicked()
{
	int i = tQueries->currentRow();
	if (i < 1) return;

	Query::Prefs tmp = queryList[i];
	queryList[i] = queryList[i-1];
	queryList[i-1] = tmp;
	tQueries->setCurrentCell(i-1, tQueries->currentColumn());

	refresh();
}

void PrefsQueries::refresh()
{
	int cnt = queryList.count();
	tQueries->setRowCount(cnt);

	for (int i = 0; i < cnt; ++i) {
		tQueries->setItem(i, 0, new QTableWidgetItem(queryList[i].title));
		tQueries->setItem(i, 1,
					   queryList[i].filterMatching ?
							   new QTableWidgetItem(QIcon(":/actions/ok.png"), tr("Yes")) :
							   new QTableWidgetItem(QIcon(":/actions/no.png"), tr("No"))
		);
	}
}

void PrefsQueries::on_bEdit_clicked()
{
	if (tQueries->currentRow() < 0) return;

	Query::Prefs &prefs = queryList[tQueries->currentRow()];

	NewQuery *dlg = new NewQuery(this, nickList);
	dlg->setQueryPrefs(prefs);
	dlg->exec();

	if (dlg->result() == QDialog::Accepted) {
		prefs = dlg->queryPrefs();
		refresh();
	}

	delete dlg;
}

void PrefsQueries::on_bRemove_clicked()
{
	if (tQueries->currentRow() < 0) return;

	QMessageBox::StandardButton result =
			QMessageBox::question(this,
								  tr("Delete query"),
								  tr("Do you really want to delete the selected query?"),
								  QMessageBox::Yes | QMessageBox::No
								 );

	if (result == QMessageBox::Yes) {
		queryList.remove(tQueries->currentRow());
		refresh();
	}
}

void PrefsQueries::on_bAdd_clicked()
{
	NewQuery *dlg = new NewQuery(this, nickList);
	dlg->exec();

	if (dlg->result() == QDialog::Accepted) {
		queryList.append(dlg->queryPrefs());
		refresh();
	}

	delete dlg;
}

void PrefsQueries::setPrefs(PrefsQueries::Prefs prefs)
{
	queryList = prefs.queryList;
	refresh();

	cbOpenDefault->setCheckState(prefs.openDefaultQueriesAtStartup ? Qt::Checked : Qt::Unchecked);
	cbSaveOpen->setCheckState(prefs.saveOpenAsDefaultOnQuit ? Qt::Checked : Qt::Unchecked);
	cbOpenGossip->setCheckState(prefs.openGossipAtStartup ? Qt::Checked : Qt::Unchecked);
}

PrefsQueries::Prefs PrefsQueries::prefs()
{
	Prefs prefs;
	prefs.queryList = queryList;
	prefs.openDefaultQueriesAtStartup = (cbOpenDefault->checkState() == Qt::Checked);
	prefs.saveOpenAsDefaultOnQuit = (cbSaveOpen->checkState() == Qt::Checked);
	prefs.openGossipAtStartup = (cbOpenGossip->checkState() == Qt::Checked);
	return prefs;
}

PrefsQueries::PrefsQueries ( QWidget* parent, const NickList &nickList_ )
		: QWidget ( parent ), Ui::prefsQueries(), nickList(nickList_)
{
	setupUi ( this );
	on_tQueries_currentCellChanged(tQueries->currentRow());
}

PrefsQueries::~PrefsQueries()
{
}


